package cn.jasonone.controller;

import java.lang.ProcessBuilder.Redirect;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.jasonone.bean.SysUser;
import cn.jasonone.service.SysUserService;

@SessionAttributes({"LOGIN_STATUS","RESOURCE_TYPE_MENU_ID"})
@Controller
public class HomeController {
	@Resource
	private SysUserService userService;
	@Value("${resourceTypeMenuId:1}")
	private Integer resourceTypeMenuId;

	@RequestMapping("/")
	public String home() {
		return "index";
	}

	@GetMapping("/login")
	public String login() {
		return "login";
	}

	@PostMapping("/login")
	public String login(SysUser user, ModelMap map) {
		SysUser login = userService.login(user);
		if (login == null) {
			return "login";
		}
		map.put("LOGIN_STATUS", login);
		map.put("RESOURCE_TYPE_MENU_ID", resourceTypeMenuId);
		return "redirect:/";
	}

	@RequestMapping("/logout")
	public String logout(HttpSession session) {
//		session.invalidate();
//		return "login";
		throw new RuntimeException();
	}

}
